﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using CodeBadger.GasMileage.Models;
using CodeBadger.GasMileage.Persistence;
using CodeBadger.GasMileage.ViewModels;

namespace CodeBadger.GasMileage.Views
{
    public partial class ListPage
    {
        public ListPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            InitializeViewModel();
        }

        private void InitializeViewModel()
        {
            var vm = new ListViewModel();
            LayoutRoot.DataContext = vm;
            vm.Load();
        }

        private void AddClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddEditPage.xaml", UriKind.Relative));
        }

        private void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listbox = (ListBox) sender;
            var entry = (NotebookEntry) listbox.SelectedItem;
            if (entry == null) return;
            var uri = "/Views/AddEditPage.xaml?id=" + entry.Id;
            NavigationService.Navigate(new Uri(uri, UriKind.Relative));
        }

        private void EmailClick(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/EmailDataPage.xaml", UriKind.Relative));
        }
    }
}